package com.kaluli.config;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
@ComponentScan(basePackages =  {"com.kaluli.project.service","com.kaluli.system.service"} )
@PropertySource({ "classpath:cache.properties" })
public class CacheConfig {
	@Autowired
	private Environment env;
	
	@Bean
	public JedisPoolConfig jedisPoolConfig(){
		
		int maxactive = new Integer(env.getProperty("redis.pool.maxTotal"));
		int maxidle = new Integer(env.getProperty("redis.pool.maxIdle"));
		JedisPoolConfig jedisconfig = new JedisPoolConfig();
		//jedisconfig.setMaxActive(maxactive);
		//jedisconfig.setMaxTotal(maxactive);
		jedisconfig.setMaxTotal(maxactive);
		jedisconfig.setMaxIdle(maxidle);
		//jedisconfig.setMaxIdle(maxidle);
		return jedisconfig;
		//config.setMaxIdle(env.getProperty("redis.pool.maxIdle"));
		
	}
	
	@Bean
	public RedisTemplate<Serializable, Serializable> redisTemplate(JedisConnectionFactory jedisConnectionFactory){
		RedisTemplate<Serializable, Serializable> redisTemplate = new RedisTemplate<Serializable, Serializable>();
		redisTemplate.setConnectionFactory(jedisConnectionFactory);
		return redisTemplate;
	}
	
	@Bean
	public JedisConnectionFactory jedisConnectionFactory(@Qualifier("jedisPoolConfig")JedisPoolConfig jedisPoolConfig){
		JedisConnectionFactory  jedisConnectionFactory = new JedisConnectionFactory();
		jedisConnectionFactory.setHostName(env.getProperty("redis.pool.host"));
		jedisConnectionFactory.setPort(new Integer(env.getProperty("redis.pool.port")));
		jedisConnectionFactory.setPassword(env.getProperty("redis.pool.password"));
		jedisConnectionFactory.setUsePool(true);
		jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
		return jedisConnectionFactory;
		
	}
	
	
	
}
